//
//  BaseView.m
//  Comma
//
//  Created by dlios on 15-1-13.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "BaseView.h"
#import "GlobalMethod.h"
@interface BaseView ()

@end

@implementation BaseView

- (instancetype)initWithLeftButton:(NSString *)leftTitle RightButton:(NSString *)rightTitle Title:(NSString *)title FooterView:(FooterViewState)footer
{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self) {
        self.headerView = [[UIView alloc] init];
        switch ([GlobalMethod getiPhoneNumber]) {
            case iPhone_4:
                [_headerView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
                break;
            case iPhone_5:
                [_headerView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
                break;
            case iPhone_6:
                [_headerView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
                break;
            case iPhone_6Plus:
                [_headerView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
                break;
            default:
                NSLog(@"未知型号");
                break;
        }
        [_headerView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
        [_headerView setBackgroundColor:BLACKCOLOR];
        [self addSubview:_headerView];
        if (leftTitle != nil) {
            [self creatLeftButtonWithTitle:leftTitle];
        }
        if (rightTitle != nil) {
            [self creatRightButtonWithTitle:rightTitle];
        }
        if (title != nil) {
            [self creatTitle:title];
        }
        switch (footer) {
            case FooterViewStateNone:
                break;
            case FooterViewStateNormal:
                [self creatFooterView];
                [self creatFooterButton];
                break;
            case FooterViewStateCustom:
                [self creatFooterView];
                break;
            default:
                break;
        }
    }
    return self;
}

// 创建左侧按钮
- (void)creatLeftButtonWithTitle:(NSString *)leftTitle
{
    self.leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_leftButton setFrame:CGRectMake(0, 20, 48, 44)];
    [_leftButton setTitle:leftTitle forState:UIControlStateNormal];
    [_leftButton setTintColor:[UIColor whiteColor]];
    [_leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:_leftButton];
}

// 创建右侧按钮
- (void)creatRightButtonWithTitle:(NSString *)leftTitle
{
    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_rightButton setFrame:CGRectMake(SCREEN_WIDTH - 48, 20, 48, 44)];
    [_rightButton setTitle:leftTitle forState:UIControlStateNormal];
    [_rightButton setTintColor:[UIColor whiteColor]];
    [_rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:_rightButton];
}

// 创建标题
- (void)creatTitle:(NSString *)title
{
    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(48, 24, SCREEN_WIDTH - 96, 40)];
    [titleLabel setText:title];
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    [self.headerView addSubview:titleLabel];
    [self bringSubviewToFront:_headerView];
}

- (void)creatFooterView
{
    self.footerView = [[UIView alloc] init];
    [_footerView setFrame:CGRectMake(0, SCREEN_HEIGHT - 49, SCREEN_WIDTH, 49)];
    [_footerView setBackgroundColor:FOOTERGRAY];
    [self addSubview:_footerView];
    [self bringSubviewToFront:_footerView];
}

- (void)creatFooterButton
{
    self.footerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    switch ([GlobalMethod getiPhoneNumber]) {
        case iPhone_6Plus:
            [_footerButton setFrame:CGRectMake(SCREEN_WIDTH / 2 - 24, 0, 48, 48)];
            break;
        default:
            [_footerButton setFrame:CGRectMake(SCREEN_WIDTH / 2 - 24, 0, 48, 48)];
            break;
    }
    [_footerButton setBackgroundImage:[UIImage imageNamed:@"Button_Releaselogo"] forState:UIControlStateNormal];
    [_footerButton addTarget:self action:@selector(footerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_footerView addSubview:_footerButton];
}

// leftButton点击事件
- (void)leftButtonAction:(id)sender
{
    
}

// rightButton点击事件
- (void)rightButtonAction:(id)sender
{
    
}

// footerButton点击事件
- (void)footerButtonAction:(id)sender
{
    
}

- (void)registerForKeyboardNotifications

{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

// 比键盘弹出先走,textfield开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.editingTextField = textField;
    if (_keyBoardDidShow) {
        [self viewWillChangeByKeyBoard];
    }
}

// 消息中心发出通知,键盘弹出
- (void)keyboardWasShown:(NSNotification *)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    self.keyBoardHeight = kbSize.height;
    self.keyBoardDidShow = YES;
    self.keyBoardAnimationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [self viewWillChangeByKeyBoard];
}

// 自定义方法,视图改变方法
- (void)viewWillChangeByKeyBoard
{
    CGFloat differ = _editingTextField.frame.origin.y + _editingTextField.frame.size.height - SCREEN_HEIGHT + _keyBoardHeight;
    if (_viewDidChange) {
        if (differ < 0) {
            [UIView animateWithDuration:0.1 animations:^{
                [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            } completion:^(BOOL finished) {
                self.viewDidChange = NO;
            }];
        }
    }
    else {
        if (differ > 0) {
            [UIView animateWithDuration:0.1 animations:^{
                [self setFrame:CGRectMake(0, - differ, SCREEN_WIDTH, SCREEN_HEIGHT)];
            } completion:^(BOOL finished) {
                self.viewDidChange = YES;
            }];
        }
    }
}

// 消息中心发出通知,键盘回收
- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
    if (_viewDidChange) {
        [UIView animateWithDuration:0.05 animations:^{
            [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        } completion:^(BOOL finished) {
            self.viewDidChange = NO;
        }];
    }
    self.keyBoardDidShow = NO;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

@end
